<template>
  <div class="login_wrap">
    <el-row>
      <el-col :span="12">
      </el-col>
      <el-col :span="12">
        <div class="box">
          <h2>登录LOGIN</h2>
          <el-form :model="lognForm" class="form" ref="loginRef" :rules="rules">
            <el-form-item label="" prop="username">
              <el-input v-model="lognForm.username" :prefix-icon="User" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input v-model="lognForm.password" :prefix-icon="Lock" type="password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" @click="onSubmit" style="width: 100%;">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { nanoid } from 'nanoid'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import axios from 'axios'
import {useRoute,useRouter} from 'vue-router'

import useUserStore from '@/store/user'

import request from '@/utils/request'

let userStore = useUserStore()

const route = useRoute()
const router = useRouter()

let lognForm = reactive({
  username: '',
  password: ''
})
let loginRef = ref()
let rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

function onSubmit() { // admin macro123
  loginRef.value.validate((valid: boolean, fields: any) => {
    if (valid) {
      request({
        url:'/admin/login',
        method:'post',
        data:lognForm
      }).then(res=>{
        console.log(res,'request')
        if(res.code == '200'){
          let settoken = `${res.data.tokenHead}${res.data.token}`
          console.log(settoken)
          localStorage.setItem('TOKEN',settoken)
          userStore.token = settoken
          userStore.username = lognForm.username
          ElMessage.success('登录成功')
          router.replace({
            path: '/home'
          })
        }else{
          ElMessage.error(res.data.message)
        }
      }).catch(err=>{
        console.log(err)
      })
    }
  })
}
</script>

<style scoped lang="less">
.login_wrap {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .box {
    width: 380px;
    position: absolute;
    top: 35vh;
    left: 65vw;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    h2 {
      color: white;
      font-size: 24px;
      margin: 10px 0px;
    }
    .form {
      padding: 40px;
    }
  }
}
</style>